<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript"  src="jquery-1.7.2.min.js"></script>
<script type="text/javascript">
	$(document).ready(function(){
		$("#btn1").attr('checked',function(){
				$.ajax({
					url: "/TEST_Project/TestServlet",
					type : "post",
					data : "command="+"go2",
					success :function(data){
						$("#test").empty();
						$("#test").append("<img src='img/DICOM-Files.gif'>");
					}				
				});
		});
	});
</script>
<script type="text/javascript">
function pause(numberMillis) {
    var now = new Date();
    var exitTime = now.getTime() + numberMillis;
    
    while (true) {
         now = new Date();
         if (now.getTime() > exitTime)
             return;
    }
}
var xhr;
function creatXhr(){
	if(window.ActiveXObject){
		xhr = new ActiveXObject("Microsoft.XMLHTTP");
	}else{
		xhr = new XMLHttpRequest();
	}
}
function test(){
	for(i=0; i<10; i++){
		setTimeout("getHello2("+i+")",3000);
	}
}
function getHello2(i){
	alert(i);
	creatXhr();
	var quertString = "i="+i;
	xhr.onreadystatechange = callback;
	xhr.open("POST","/TEST_Project/TestServlet",true);
	xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xhr.send(quertString);
}
function callback(){
	if(xhr.readyState==4){
		if(xhr.status==200){
			var restTxt = xhr.responseText;
			document.getElementById("resultLayer").innerHTML = restTxt;
		}else{
			alert("요청에 대한 처리를 하지 못햇습니다.: "+xhr.status);
		}
	}
}
</script>
</head>
<body>
<form action="" method="post">
<table border="1"> 
	<tr>
		<td>	
		<c:choose>
			<c:when test="${requestScope.flag eq 'Y' }">
		  <input type="checkbox" name="go" value="Y" id="btn1"<c:if test="${requestScope.flag eq 'Y' }">checked="checked"</c:if>>
				<img src="img/DICOM-Files.gif">
			</c:when>
			<c:otherwise>
			<input type="checkbox">
				<img src="img/1.jpg">
			</c:otherwise>
		 </c:choose></td>
	</tr>
	<tr>
		<td>
			<span id="test">
				<img src="img/1.jpg">
			</span>
		</td>
	</tr>
</table>
<button id="btn" name="command" value="go">시작</button>
<button id="btn2" name="command" value="stop">스톱</button>
<button onclick="test()">클릭</button>
<div id="resultLayer"></div>
</form>
</body>
</html>